package ext.tianma.util;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

import org.apache.log4j.Logger;

import com.ptc.core.ui.validation.DefaultSimpleValidationFilter;
import com.ptc.core.ui.validation.UIValidationCriteria;
import com.ptc.core.ui.validation.UIValidationKey;
import com.ptc.core.ui.validation.UIValidationResultSet;
import com.ptc.core.ui.validation.UIValidationStatus;

import ext.tianma.listener.CusPartListenerService;
import wt.log4j.LogR;
import wt.org.WTPrincipal;
import wt.org.WTUser;
import wt.session.SessionHelper;
import wt.util.WTException;

/**
 * Windchill验证定制
 *
 * xconfmanager -s
 * wt.services/svc/default/com.ptc.core.ui.validation.DefaultUIComponentValidator/DCJValidateAdmin/null/0=ext.tianma.util.DCJValidateAdmin/duplicate
 * -t codebase/service.properties –p
 *
 * 
 */
public class DCJValidateAdmin extends DefaultSimpleValidationFilter {
	/** Log4j Logger. */
	private static final Logger LOGGER = LogR.getLogger(CusPartListenerService.class.getName());

	/**
	 * 处理多个Action的的验证
	 * 
	 * @param UIValidationKey
	 *            要验证的action
	 * @param UIValidationCriteria
	 *            验证标志实体类
	 * @param Locale
	 *            本地化的
	 */
	public UIValidationStatus preValidateAction(UIValidationKey key, UIValidationCriteria criteria) {
		LOGGER.debug("start====>>>>");
		UIValidationStatus ui = UIValidationStatus.ENABLED;
		String strComponentID = key.getComponentID();
		LOGGER.debug("DCJValidateAdmin————————>");
		// 获得参与者
		WTPrincipal wtp;
		try {
			wtp = (WTPrincipal) SessionHelper.manager.getPrincipal();
			// 获得验证结果
			UIValidationResultSet rs = new UIValidationResultSet();
			// 判断参与者是不是管理员
			wt.org.WTUser user = (WTUser) wtp;
			LOGGER.debug("user========>>>" + user);
			List<String> groupNameList = new ArrayList<String>();
			Enumeration groupNameEA = user.parentGroupNames();
			while (groupNameEA.hasMoreElements()) {

				String groupName = (String) groupNameEA.nextElement();
				LOGGER.debug("groupName11================>>>" + groupName);
				LOGGER.debug("groupName22================>>>" + groupName.toString().trim().equals("Asi-BOM工程师"));
				// LOGGER.debug("DCJValidateAdmin————————>performFullPreValidation:" +
				// groupName);
				groupNameList.add(groupName);
				if (groupName.toString().trim().equals("CTP-BOM工程师")
						|| groupName.toString().trim().equals("Administrators")
						|| groupName.toString().trim().equals("Asi-BOM工程师")
						|| groupName.toString().trim().equals("LTPS-BOM工程师")
						|| groupName.toString().trim().equals("Asi（上海）-BOM工程师")
						|| groupName.toString().trim().equals("无源-BOM工程师")) {
					LOGGER.debug("可见====》》》》");
					return ui = UIValidationStatus.ENABLED;
				} else {
					LOGGER.debug("=======>>>>>");
					ui = UIValidationStatus.HIDDEN;
				}
			}
		} catch (WTException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//
		return ui;
	}
}
